package com.tengfei.main.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tengfei.main.course.mapper.CourseMapper;
import com.tengfei.main.index.domain.Banner;
import com.tengfei.main.index.domain.Notice;
import com.tengfei.main.index.domain.vo.BannerVo;
import com.tengfei.main.index.domain.vo.NoticeVo;
import com.tengfei.main.index.mapper.BannerMapper;
import com.tengfei.main.index.mapper.NoticeMapper;
import com.tengfei.main.user.domain.vo.course.UserCourseVo;
import com.tengfei.main.user.service.IUserIndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author feige
 * @version v1.0
 * @date 2022-08-14-17:32
 * @description 用户首页
 */

@Service
public class UserIndexServiceImpl implements IUserIndexService {

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private BannerMapper bannerMapper;

    // 轮播图展示
    private static final Integer BANNER_SHOW = 1;

    /**
     * 热门课程
     *
     * @param size
     * @return
     */
    @Override
    public List<UserCourseVo> getHotCourseList(Integer size) {
        if (size.intValue() <= 0) {
            size = 5;
        }
        List<UserCourseVo> userCourseVos = courseMapper.selectHotAndViewCount(size);
        return userCourseVos;
    }

    /**
     * 最新课程
     *
     * @param size
     * @return
     */
    @Override
    public List<UserCourseVo> getNewCourseList(Integer size) {
        if (size.intValue() <= 0) {
            size = 5;
        }
        List<UserCourseVo> userCourseVos = courseMapper.selectNewAndViewCount(size);
        return userCourseVos;
    }

    /**
     * 首页轮播图
     *
     * @param size
     * @return
     */
    @Override
    public List<BannerVo> getBannerList(Integer size) {
        List<BannerVo> bannerVos = bannerMapper.selectVoList(new LambdaQueryWrapper<Banner>()
                .eq(Banner::getBnIsShow, BANNER_SHOW)
                .orderByDesc(Banner::getUpdateTime)
                .last(" limit " + size));
        return bannerVos;
    }

    /**
     * 首页通知公告
     *
     * @param size
     * @return
     */
    @Override
    public List<NoticeVo> getNoticeList(Integer size, Integer type) {
        List<NoticeVo> noticeVos = noticeMapper.selectVoList(new LambdaQueryWrapper<Notice>()
                .eq(ObjectUtil.isNotNull(type), Notice::getNoType, type)
                .orderByDesc(Notice::getNoIsTop)
                .orderByDesc(Notice::getUpdateTime)
                .last(" limit " + size));
        return noticeVos;
    }
}
